﻿using System;
using System.Collections.Generic;
using RiskMan.Config;
using RiskMan.Models;
using TheControllerRisk.Providers;
using RiskMan.DataClass.Models;
using System.Collections.ObjectModel;
using TheDoubleStop.Models;
using RiskMan.DataClass.Models.Level;
using RiskMan.DataClass.Models.Gilevskiy;

namespace RiskMan.DataClass
{
    public partial class Data
    {
        private DDEProvider _ddeServer;

        public List<CurrentParamModel> CurrentList = new List<CurrentParamModel>();
        public List<DealModel> DealList = new List<DealModel>();
        public List<EmitentLimitModel> EmitentLimiteList = new List<EmitentLimitModel>();
        public List<StopOrderModel> StopOrderList = new List<StopOrderModel>();
        public List<OrderModel> OrderList = new List<OrderModel>();
        public List<AllTradesModels> AllTradesList = new List<AllTradesModels>();
        public ObservableCollection<LimitByAccountModel> LimitebyAccount = new ObservableCollection<LimitByAccountModel>();
        public List<PositionToClientAccountModel> FortsPositionsList = new List<PositionToClientAccountModel>();
        public List<PortfelItem> Portfel = new List<PortfelItem>();
        public List<EminentLimitByMoneySredstva> LimiteByMoneySredstva = new List<EminentLimitByMoneySredstva>();

        public QuikItems T2QItems = new QuikItems();

        #region data for StopView

        /// <summary>
        /// массив стопов пользователя
        /// </summary>
        public ObservableCollection<StopListModel> StopList = new ObservableCollection<StopListModel>();

        public List<OrderModel> TakeOrderList = new List<OrderModel>();
        /// <summary>
        /// сделки по которым устанавливается стоп-лосс
        /// </summary>
        public List<DealModel> DealSOrderList = new List<DealModel>();

        /// <summary>
        /// стоп-лосс выставленный по сделке
        /// </summary>
        public List<StopOrderModel> StopLossOrderList = new List<StopOrderModel>();

        #endregion

        #region data for Levels


        /// <summary>
        /// массив настроеных счетов и инструментов для лесенки
        /// </summary>
        public ObservableCollection<LevelItem> LevelItems = new ObservableCollection<LevelItem>();

        #endregion

        #region data for Gilevskiy

        public ObservableCollection<RobotGilevskiyItem> RobotGilevskiyItems = new ObservableCollection<RobotGilevskiyItem>();
        public ObservableCollection<TimeFrameItem> TimeFrameItems = new ObservableCollection<TimeFrameItem>();

        #endregion

        public Data()
        {
            _ddeServer = new DDEProvider(this);
            if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
                _ddeServer.Connect();
        }

        public double GetEnterPrice(string seccode, string clientcode, string account, double currpos)
        {
            double result = 0;

            if (currpos > 0)
                for (int i = DealList.Count - 1; i >= 0; i--)
                    if (DealList[i].Account == account &&
                        DealList[i].ClientCode == clientcode &&
                        DealList[i].Seccode == seccode &&
                        DealList[i].Operation == Cfg.Buy)
                        return DealList[i].Price;

            if (currpos < 0)
                for (int i = DealList.Count - 1; i >= 0; i--)
                    if (DealList[i].Account == account &&
                        DealList[i].ClientCode == clientcode &&
                        DealList[i].Seccode == seccode &&
                        DealList[i].Operation == Cfg.Sell)
                        return DealList[i].Price;

            return result;
        }

        public double GetSlActive(string seccode, string clientcode, string account, double currpos)
        {
            double result = 0;

            if (currpos > 0)
                for (int i = StopOrderList.Count - 1; i >= 0; i--)
                    if (StopOrderList[i].TradeAccount == account &&
                        StopOrderList[i].ClientCode == clientcode &&
                        StopOrderList[i].Seccode == seccode &&
                        StopOrderList[i].Operation == Cfg.Sell)
                        return StopOrderList[i].StopPrice;

            if (currpos < 0)
                for (int i = StopOrderList.Count - 1; i >= 0; i--)
                    if (StopOrderList[i].TradeAccount == account &&
                        StopOrderList[i].ClientCode == clientcode &&
                        StopOrderList[i].Seccode == seccode &&
                        StopOrderList[i].Operation == Cfg.Buy)
                        return StopOrderList[i].StopPrice;

            return result;
        }

        public bool GetTypeAccount(string clientCode)
        {
            for (int i = 0; i < Portfel.Count; i++)
            {
                if (Portfel[i].CodeClient == clientCode)
                    return false;
            }
            return true;
        }

        public double GetTypeAccountMoney(string account, string clientcode)
        {
            for (int i = 0; i < Portfel.Count; i++)
            {
                if (Portfel[i].Firm != "NB0253900000")
                {
                    if (Portfel[i].SrochAccount == account)
                        return Portfel[i].EnterSredstva;
                    if (Portfel[i].CodeClient == clientcode)
                        return Portfel[i].EnterSredstva;
                }
            }
            return 0;
        }

        public double GetMoney(string clientcode)
        {
            for (int i = 0; i < LimiteByMoneySredstva.Count; i++)
            {
                if (LimiteByMoneySredstva[i].Valute == "SUR")
                {
                    if (LimiteByMoneySredstva[i].CodeClient == clientcode && LimiteByMoneySredstva[i].Group == "GLSP")
                        return LimiteByMoneySredstva[i].Dostupno;

                    if (LimiteByMoneySredstva[i].CodeClient == clientcode && LimiteByMoneySredstva[i].Group == "EQTV")
                        return LimiteByMoneySredstva[i].Dostupno;
                }
            }
            return 0;
        }

        public List<string> ListPath = new List<string>();

        public String GetAccount(string clientcode)
        {
            if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
            {
                for (int i = 0; i < T2QItems.Count; i++)
                {
                    for (int j = 0; j < T2QItems[i].TradeList.Count; j++)
                        if (clientcode == T2QItems[i].TradeList[j].ClientCode)
                            return T2QItems[i].TradeList[j].Account;
                }
            }
            return "";
        }

        public StopOrderModel GetData(StopOrderModel stopOrderModel)
        {
            for (int i = 0; i < StopOrderList.Count; i++)
            {
                if (StopOrderList[i].TID == stopOrderModel.TID)
                    stopOrderModel = StopOrderList[i];
            }
            return stopOrderModel;
        }

        public double GetPriceClose(string seccode)
        {
            for (int i = 0; i < CurrentList.Count; i++)
            {
                if (CurrentList[i].Seccode == seccode)
                    return Math.Floor(CurrentList[i].ClosePrice * GetLots(seccode) / 2) / GetLots(seccode);
            }
            return 0;
        }

        public double GetDostupnoBalance(string clientCode)
        {
            double result = 0;

            for (int i = 0; i < Portfel.Count; i++)
            {
                if (Portfel[i].CodeClient == clientCode)
                    return Portfel[i].OnBuy;
            }

            return result;
        }

        internal double GetLaverage(string clientcode)
        {
            for (int i = 0; i < Portfel.Count; i++)
            {
                if (Portfel[i].CodeClient == clientcode)
                    return Portfel[i].Laverage + 1;
            }
            return 1;
        }



        internal void Clear()
        {
            CurrentList = new List<CurrentParamModel>();
            DealList = new List<DealModel>();
            EmitentLimiteList = new List<EmitentLimitModel>();
            FortsPositionsList = new List<PositionToClientAccountModel>();
            StopOrderList = new List<StopOrderModel>();
            OrderList = new List<OrderModel>();
            LimitebyAccount = new ObservableCollection<LimitByAccountModel>();
            Portfel = new List<PortfelItem>();
            LimiteByMoneySredstva = new List<EminentLimitByMoneySredstva>();
        }

        internal double GetStepPrice(string seccode)
        {
            for (int i = 0; i < CurrentList.Count; i++)
            {
                if (CurrentList[i].Seccode == seccode)
                {
                    return CurrentList[i].StepPrice;
                }
            }
            return 0;
        }

        internal List<OrderModel> GetOrderList(string account, string clientcode)
        {
            List<OrderModel> result = new List<OrderModel>();

            if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
            {
                for (int i = 0; i < OrderList.Count; i++)
                {
                    if (OrderList[i].Account == account &&
                        OrderList[i].ClientCode == clientcode &&
                        OrderList[i].Status == Cfg.Active)
                    {
                        result.Add(OrderList[i]);
                    }
                }
            }



            return result;
        }

        internal List<OrderModel> GetOrderListAll(string account, string clientcode)
        {
            List<OrderModel> result = new List<OrderModel>();

            if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
                for (int i = 0; i < OrderList.Count; i++)
                {
                    if (OrderList[i].Account == account &&
                         OrderList[i].ClientCode == clientcode)
                    {
                        result.Add(OrderList[i]);
                    }
                }
           

            return result;
        }

        internal List<StopOrderModel> GetStopOrderList(string account, string clientcode)
        {
            List<StopOrderModel> result = new List<StopOrderModel>();
            if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
            {
                for (int i = 0; i < StopOrderList.Count; i++)
                {
                    if (StopOrderList[i].TradeAccount == account &&
                        StopOrderList[i].ClientCode == clientcode)
                    {
                        result.Add(StopOrderList[i]);
                    }
                }
            }

           

            return result;
        }

        internal List<StopOrderModel> GetStopOrderListActive(string account, string clientcode)
        {
            List<StopOrderModel> result = new List<StopOrderModel>();

            for (int i = 0; i < StopOrderList.Count; i++)
            {
                if (StopOrderList[i].TradeAccount == account &&
                     StopOrderList[i].ClientCode == clientcode &&
                     StopOrderList[i].Status == Cfg.Active)
                {
                    result.Add(StopOrderList[i]);
                }
            }

            return result;
        }

    }
}